// getAccuauthSDK
import 'package:liveness_plugin/liveness_plugin.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:smartloan_mdc/extension/extension.dart';
import 'package:smartloan_mdc/res/string.dart';
import 'package:smartloan_mdc/utils/locator/app_config.dart';
import 'package:smartloan_mdc/utils/locator/locator.dart';

import '../javascript_channel.dart';

class GetAccuauthSDK extends JavaScriptChannel implements LivenessDetectionCallback {
  @override
  List<String> get actions => ['getAccuauthSDK'];

  @override
  onReceive(Map newMsg) async {
    var permissionStatus = await Permission.camera.request();
    if (permissionStatus.isPermanentlyDenied) {
      AppStr.permissionObtain.toast();
      return;
    }else if(permissionStatus.isDenied){
      return;
    }
    LivenessPlugin.initSDK2(
      locator<AppConfig>().appConfigModel.extraMeta!.advanceAccessKey!,
      locator<AppConfig>().appConfigModel.extraMeta!.advanceSecretKey!,
      locator<AppConfig>().appConfigModel.extraMeta!.advanceFace!,
    );
    LivenessPlugin.startLivenessDetection(this);
  }

  @override
  void onGetDetectionResult(bool isSuccess, Map? resultMap) {
    callBack(isSuccess, isSuccess ? {
      'livenessId': resultMap?['livenessId'],
      'file': resultMap?['base64Image']
    } : {
      'errorMsg': resultMap?['message']
    });
  }

}